const express=require('express')
const path=require('path')
const fs=require('fs')
let app=express()
app.listen(3000,()=>{
    console.log('3000ok');
})



// 引入uuid
// console.log(require("uuid"));
let {v4}=require("uuid")
/* {
    v1: [Getter],
    v3: [Getter],
    v4: [Getter],
    v5: [Getter],
    NIL: [Getter],
    version: [Getter],
    validate: [Getter],
    stringify: [Getter],
    parse: [Getter]
} */
// console.log(uuid.NIL);
// console.log(uuid.parse("00000000-0000-0000-0000-000000000000"));
// console.log(v4());// 315da389-57eb-4bf5-89a6-4426b7e9b63e



app.get("/register",(req,res)=>{
    res.sendFile(path.join(__dirname,"./login.html"))
})


// 引入 formidable
let formidable=require("formidable")


app.post("/register",(req,res)=>{

    // 设置文件临时上传的目录
    let  form=formidable({
        uploadDir: path.join(__dirname,"./tempdir")
    })

    // 解析信息
    form.parse(req,(err,fields,files)=>{
        
        // 错误拦截
        if(err){
            res.send("文件上传失败")
            return
        }




        let oldpath=files.touxiang.filepath

        let ext= path.extname(files.touxiang.originalFilename)

        // -----------------------------------
        let newname=v4()

        let newpath=path.join(__dirname,"./uploads", newname )+ext

        fs.renameSync(oldpath,newpath)


        res.send("文件上传成功")

    })

})
